#!/bin/bash

# refer  spf13-vim bootstrap.sh`
BASEDIR=$(dirname $0)
cd $BASEDIR
CURRENT_DIR=`pwd`

lnif() {
    if [ -e "$1" ]; then
        ln -sf "$1" "$2"
    fi
}


echo "Step1: backing up current vim config"
today=`date +%Y%m%d`
for i in $HOME/.vim $HOME/.vimrc $HOME/.gvimrc $HOME/.vimrc.bundles; do [ -e $i ] && [ ! -L $i ] && mv $i $i.$today; done
for i in $HOME/.vim $HOME/.vimrc $HOME/.gvimrc $HOME/.vimrc.bundles; do [ -L $i ] && unlink $i ; done


echo "Step2: setting up symlinks"
lnif $CURRENT_DIR/vimrc $HOME/.vimrc
lnif $CURRENT_DIR/vimrc.bundles $HOME/.vimrc.bundles
lnif "$CURRENT_DIR/" "$HOME/.vim"


echo "Step3: update/install plugins using Vundle"
system_shell=$SHELL
export SHELL="/bin/sh"
vim +PlugInstall! +PlugClean! +qall
export SHELL=$system_shell


echo "Step4: compile YouCompleteMe"
echo "It will take a long time, just be patient!"
echo "If error,you need to compile it yourself"
if [ ! -d "$CURRENT_DIR/plugged/YouCompleteMe" ]; then
    mkdir -p $CURRENT_DIR/plugged/YouCompleteMe
fi
# make sure cmake is higher than 3.4
#if [ ! -d "/usr/local/cmake" ]; then
    #sudo mkdir -p /usr/local/cmake/download
    #cd /usr/local/cmake/download
    #wget https://cmake.org/files/v3.7/cmake-3.7.1.tar.gz
    #gunzip cmake-3.7.1.tar.gz
    #tar xf cmake-3.7.1.tar.gz
    #cd cmake-3.7.1
    #./configure --prefix=/usr/local/cmake
    #sudo make
    #sudo make install

    #sudo mkdir -p /usr/local/clang/download
    #cd /usr/local/clang/download
    #git clone http://llvm.org/git/llvm.git
    #cd llvm/tools
    #git clone http://llvm.org/git/clang.git
    #cd ../projects
    #git clone http://llvm.org/git/compiler-rt.git
    #cd ../tools/clang/tools/extra
    #git clone http://llvm.org/git/clang-tools-extra.git

    #cd /usr/local/clang/
    #/usr/local/cmake/bin/cmake -G "Unix Makefiles" /usr/local/clang/llvm
    #sudo make
    #sudo make install
#fi
echo "cd $CURRENT_DIR/plugged/YouCompleteMe && python install.py --clang-completer"
cd $CURRENT_DIR/plugged/YouCompleteMe

# without clang, need to add clang
#python install.py --clang-completer

# After installed clang, only need to exec below command
python install.py

echo "step5: generate the symbol link"
lnif $CURRENT_DIR/bashrc $HOME/.bashrc
lnif $CURRENT_DIR/bashrc.c4dev $HOME/.bashrc.c4dev
lnif $CURRENT_DIR/dircolors $HOME/.dircolors
lnif $CURRENT_DIR/alias $HOME/.alias

#sudo /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

echo "Install Done!"
